<html>
	<head>
		<meta name="description" content="Das beste Musik Portal der Welt" content="text/html; utf-8"/>
	</head>
</html>

<?php
session_start();
$userid = $_SESSION['userid'];
$path = "../music/";
require_once('../getid3/getid3.php');
function mysqlConnect(){
	include('../config/config.inc.php');	
	$db = new mysqli($mysql_host, $mysql_user, $mysql_pass, $mysql_dbname);	
	return $db;
}

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
	$name = $_FILES['track']['name'];
	$size = $_FILES['track']['size'];
	if(strlen($name))
	{
		if($_FILES['track']['type'] == "audio/mp3")
		{
			if($size<(24000000)) // File size max ~24 MB
			{
				$actual_file_name = time().".mp3";
				$tmp = $_FILES['track']['tmp_name'];
				if(move_uploaded_file($tmp, $path.$actual_file_name))
				{
					$db = mysqlConnect();
					$getID3 = new getID3;
					// Get id3 tag version
					$link = $actual_file_name;
					$tag = $getID3->analyze("../music/".$actual_file_name);
					$length = $tag['playtime_string'];
					
					//Author
					if(isset($tag['id3v2']['comments']['artist'][0]))
						$author = $tag['id3v2']['comments']['artist'][0];
					elseif(isset($tag['id3v2']['artist'][0]))
						$author = $tag['id3v2']['artist'][0];
					else
						$author = "";
					//Title
					if(isset($tag['id3v2']['comments']['title'][0]))
						$title = $tag['id3v2']['comments']['title'][0];
					elseif(isset($tag['id3v2']['title'][0]))
						$title = $tag['id3v2']['title'][0];
					else
						$title = "";
					//Album		
					if(isset($tag['id3v2']['comments']['album'][0]))
						$album = $tag['id3v2']['comments']['album'][0];
					elseif(isset($tag['id3v2']['album'][0]))
						$album = $tag['id3v2']['album'][0];
					else
						$album = "";
					//Genre	
					if(isset($tag['id3v2']['comments']['genre'][0]))
						$genre = $tag['id3v2']['comments']['genre'][0];
					elseif(isset($tag['id3v2']['genre'][0]))
						$genre = $tag['id3v2']['genre'][0];
					else
						$genre = "";
					//Year	
					if(isset($tag['id3v2']['comments']['year'][0]))
						$year = $tag['id3v2']['comments']['year'][0];
					elseif(isset($tag['id3v2']['year'][0]))
						$year = $tag['id3v2']['year'][0];
					else
						$year = "";
					
					if(!empty($_POST['upload_interpret'])){
						$author = $_POST['upload_interpret'];						
					}
					if(!empty($_POST['upload_title'])){
						$title = $_POST['upload_title']; 						
					}
					if(!empty($_POST['upload_album'])){
						$album = $_POST['upload_album'];						
					}
					if(!empty($_POST['upload_genre'])){
						$genre = $_POST['upload_genre'];						
					}
					if(!empty($_POST['upload_year'])){
						$year = $_POST['upload_year'];						
					}

					$query = "INSERT INTO `music`(`userid`, `title`, `author`, `album`, `genre`, `year`, `length`, `link`) VALUES ('$userid','$title','$author','$album','$genre','$year', '$length', '$link');"; 
					$db->query($query);
					echo "Datei erfolgreich hochgeladen!";
					echo '<meta http-equiv="refresh" content="2; URL=index.php">';
				}
				else
				echo "Failed!";
			}
			else
			echo "Maximale Datei gr&ouml;&sszlig;e: 10 MB"; 
		}
		else
		echo "Unerlaubtes Dateiformat!"; 
	}
	else
	echo "Bitte w&auml;hlen Sie eine Datei aus!";
	exit;
}
?>